/* 验证用户名*/
function verifyNickname() {
    var nickname = $("input[name='nickname']").val();
    if (!nickname) {
        $("span[role='nickname']").hide();
        $("div[role='nickname']").show();
        $("div[role='nickname']").attr("isnull", "1");
    } else {
        $("div[role='nickname']").hide();
        $("div[role='nickname']").attr("isnull", "2");
        $("span[role='nickname']").show();
    }
}

/* 验证邮箱*/
function verifyEmail() {
    var email = $("input[name='email']").val();
    if (!email) {
        $("span[role='email']").hide();
        $("div[role='email']").html("注意:邮箱不能为空!");
        $("div[role='email']").attr("isnull", "1");
        $("div[role='email']").show();
    } else {
        if (!(/^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$/.test(email))) {
            $("span[role='email']").hide();
            $("div[role='email']").html("注意:邮箱格式不正确!");
            $("div[role='email']").attr("isnull", "1");
            $("div[role='email']").show();
        } else {
            var result = verifyAccountIsExist(email);
            if (result) {
                $("span[role='email']").hide();
                $("div[role='email']").html("注意:邮箱已经被注册!");
                $("div[role='email']").attr("isnull", "1");
                $("div[role='email']").show();
            } else {
                $("div[role='email']").hide();
                $("div[role='email']").attr("isnull", "2");
                $("span[role='email']").show();
            }
        }
    }
}

/* 验证手机号码*/
function verifyPhone() {
    var phone = $("input[name='phone']").val();
    if (!phone) {
        $("span[role='phone']").hide();
        $("div[role='phone']").html("注意:手机号不能为空!");
        $("div[role='phone']").attr("isnull", "1");
        $("div[role='phone']").show();
        $("span.verifycode > button").attr("disabled", "disabled");
    } else {
        if (!(/^1[0-9]\d{9}$/.test(phone))) {
            $("span[role='phone']").hide();
            $("div[role='phone']").html("注意:手机号格式不正确!");
            $("div[role='phone']").attr("isnull", "1");
            $("div[role='phone']").show();
            $("span.verifycode > button").attr("disabled", "disabled");
        } else {
            var result = verifyAccountIsExist(phone);
            if (result) {
                $("span[role='phone']").hide();
                $("div[role='phone']").html("注意:手机号已经被注册!");
                $("div[role='phone']").attr("isnull", "1");
                $("span.verifycode > button").attr("disabled", "disabled");
                $("div[role='phone']").show();
            } else {
                $("div[role='phone']").hide();
                $("div[role='phone']").attr("isnull", "2");
                $("span[role='phone']").show();
                $("span.verifycode > button").removeAttr("disabled");
            }
        }
    }
}


/*验证密码*/
function verifyPassword() {
    var password = $("input[name='password']").val();
    if (!password) {
        $("div[role='password']").html("注意:密码不能为空!");
        $("div[role='password']").attr("isnull", "1");
        $("div[role='password']").show();
        $("span[role='password']").hide();
    } else {
        var passwordrepeat = $("input[name='passwordrepeat']").val();
        if (!passwordrepeat) {
            $("div[role='passwordrepeat']").html("注意:确认密码不能为空! ");
            $("div[role='passwordrepeat']").attr("isnull", "1");
            $("div[role='passwordrepeat']").show();
            $("span[role='password']").hide();
        } else {
            if (passwordrepeat != password) {
                $("div[role='password']").html("两次输入的密码不一致!");
                $("div[role='password']").attr("isnull", "1");
                $("div[role='password']").show();
                $("span[role='password']").hide();
            } else {
                $("div[role='password']").hide();
                $("div[role='password']").attr("isnull", "2");
                $("span[role='password']").show();
                $("div[role='passwordrepeat']").hide();
                $("div[role='passwordrepeat']").attr("isnull", "2");
                $("span[role='passwordrepeat']").show();
            }
        }
    }
}

/*验证确认密码*/
function verifyPasswordrepeat() {
    var passwordrepeat = $("input[name='passwordrepeat']").val();
    if (!passwordrepeat) {
        $("div[role='passwordrepeat']").html("注意:确认密码不能为空! ");
        $("div[role='passwordrepeat']").attr("isnull", "1");
        $("div[role='passwordrepeat']").show();
        $("span[role='passwordrepeat']").hide();
    } else {
        var password = $("input[name='password']").val();
        if (!password) {
            $("div[role='password']").html("注意:密码不能为空!");
            $("div[role='password']").attr("isnull", "1");
            $("div[role='password']").show();
            $("span[role='passwordrepeat']").hide();
        } else {
            if (passwordrepeat != password) {
                $("div[role='passwordrepeat']").html("两次输入的密码不一致!");
                $("div[role='passwordrepeat']").attr("isnull", "1");
                $("div[role='passwordrepeat']").show();
                $("span[role='passwordrepeat']").hide();
            } else {
                $("div[role='passwordrepeat']").hide();
                $("div[role='passwordrepeat']").attr("isnull", "2");
                $("span[role='passwordrepeat']").show();
                $("div[role='password']").hide();
                $("div[role='password']").attr("isnull", "2");
                $("span[role='password']").show();
            }
        }
    }
}

/*验证是否存在*/
function verifyAccountIsExist(account) {
    let result = true;
    $.ajax({
        url: "/user/verifyAccount", type: "get", async: false, data: {account: account}, success: function (data) {
            result = data["data"]["is_exist"];
        }
    });
    return result;
}


function register() {
    var arr = $("div[isnull]");
    for (var i = 0, l = arr.length; i < l; i++) {
        if (arr[i].attributes.isnull.nodeValue != "2") {  //不等于2 则肯定是等于0的; 那现在就把等于0的给揪出来;
            var role = arr[i].attributes.role.nodeValue;
            $("div[role='" + role + "']").html("注意:此处不能为空! ");
            $("div[role='" + role + "']").attr("isnull", "1");
            $("div[role='" + role + "']").show();
            $("span[role='" + role + "']").hide();
            return;
        }
    }

    $.ajax({
        url: '/user/register',
        data: $('#userRegisterForm').serialize(),
        type: "POST",
        success: function (data) {
            if (data["data"]["result"] == "ok") {
                window.location.href = "sign_in";
                $.scojs_message('注册成功,请登录!', $.scojs_message.TYPE_OK);
            } else {
                $.scojs_message('注册失败,请稍后重试,或联系管理员!', $.scojs_message.TYPE_ERROR);
            }
        }
    });
}


